﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dicom.Network.Client;
using Dicom.Network;
using System.Threading;
namespace WindowsFormsApplication9
{
    public partial class cstore : Form
    {
        public cstore()
        {
            InitializeComponent();
        }
        List<string> filechuyen = new List<string>();
        private void cstore_Load(object sender, EventArgs e)
        {
            button2.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            OpenFileDialog file = new OpenFileDialog();
            file.Multiselect = true;
            if (file.ShowDialog() == DialogResult.OK)
            {
                foreach (string filename in file.FileNames)
                {
                    if (filechuyen.Contains(filename) == false)
                    {
                        filechuyen.Add(filename);
                    }
                    textBox1.Text += filename + ",";

                }
                button2.Enabled = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                CStoreClient client = new CStoreClient();
                client.ConnectTimeout = 20;
                client.DimseTimeout = 20;
                client.MaxPduSize = Convert.ToUInt16(30);
                client.SocketTimeout = 20;
                client.CalledAE = "CONQUESTSRV1";
                client.CallingAE = "CONQUESTSRV1";
                client.Priority = DcmPriority.High;
                ThreadPool.QueueUserWorkItem(new WaitCallback(connect), client);
                //client.AddFile(textBox1.Text.ToString().Trim());
                foreach (string filename in filechuyen)
                {
                    client.AddFile(filename.Trim());
                }
                client.OnCStoreResponseReceived = delegate(CStoreClient clients, CStoreRequestInfo info)
                {
                    thongbao(info.Status.Description);
                };
                client.OnCStoreRequestBegin = delegate(CStoreClient clients, CStoreRequestInfo info)
                {
                    thongbao(info.Status.Description);
                };
                client.OnCStoreRequestFailed = delegate(CStoreClient clients, CStoreRequestInfo info)
                {
                    thongbao(info.Status.Description);
                };
                client.OnCStoreRequestProgress = delegate(CStoreClient clients, CStoreRequestInfo info, DcmDimseProgress progress)
                {
                    thongbao("total time:" + progress.EstimatedBytesTotal.ToString());
                };
                client.OnCStoreClosed = delegate(CStoreClient clients)
                {
                    client.Close();
                    //textBox1.Text = "";
                };
            }
            catch (Exception exx)
            {

            }
            //textBox1.Text = "";
        }
        public delegate void thongbaotest(string chuoi);
        private void thongbao(string p)
        {
            // throw new NotImplementedException();

            if (listBox1.InvokeRequired == true)
            {
                thongbaotest test = new thongbaotest(chuoihienthi);
                Invoke(test, new object[] { p });
            }
        }
        private void chuoihienthi(string chuoi)
        {
            listBox1.Items.Add(chuoi);
        }
        private void connect(object s)
        {
            CStoreClient client = (CStoreClient)s;
            client.Connect("127.0.0.1", 5678, DcmSocketType.TCP);

        }
    }
}
